<template>
	<view>
		<view class="product-window"
			:class="(attr.cartAttr === true ? 'on' : '') + ' ' + (iSbnt?'join':'') + ' ' + (iScart?'joinCart':'')">
			<view class="textpic acea-row row-between-wrapper">
				<view class="pictrue" @click="showImg()">
					<image :src="attrTemp.productSelect.image"></image>
				</view>
				<view class="text">
				
					<view class="money">
						<view class="acea-row row-middle">
							<text>￥<text class="num">{{day_price_temp}}</text>/天</text>
						</view>
					</view>
					<view class="sku" >
						已选择: {{lease_mode_value_temp}};{{sku}};{{total_day_value_temp}}
					</view>
					<view class="leased" @click="showLeased">租后: 续租/买断/归还 ></view>
				</view>
				<view class="iconfont icon-guanbi" @click="closeAttr"></view>
			</view>
			<view class="rollTop">
				<view class="productWinList">
					<view class="item">
						<view class="title">租赁方式</view>
						<view class="listn acea-row row-middle">
							<view class="itemn" :class="lease_mode_temp === itemn.id ? 'on sku-select' : ''"
								v-for="(itemn, indexn) in storeInfo.lease_mode" @click="tapMode(indexn)"
								:key="indexn">
								{{ itemn.name }}
							</view>
						</view>
					</view>
					
					<view class="item" v-for="(item, indexw) in attrTemp.productAttr" :key="indexw">
						<view class="title">{{ item.attr_name }}</view>
						<view class="listn acea-row row-middle">
							<view class="itemn" :class="item.index === itemn.attr ? 'on sku-select' : ''"
								v-for="(itemn, indexn) in item.attr_value" @click="tapAttr(indexw, indexn)"
								:key="indexn">
								{{ itemn.attr }}
							</view>
						</view>
					</view>
				
					<view class="item">
						<view class="title">总租期</view>
						<view class="listn acea-row row-middle">
							<view class="itemn" :class="total_day_temp === itemn.value ? 'on sku-select' : ''" @click="tapDay(indexn)"
								v-for="(itemn, indexn) in storeInfo.total_day" 
								:key="indexn">
								{{ itemn.name }}
							</view>
						</view>
					</view>
					
		<!-- 			<view class="item">
						<view class="title">总租赁期数</view>
						<view class="listn acea-row row-middle">
							<view class="itemn on">
								{{storeInfo.routine_periods }}
							</view>
						</view>
					</view> -->
					
					<view class="item">
						<view class="title">意外险</view>
						<view class="listn acea-row row-middle">
							<view class="itemn on sku-select">
								¥{{storeInfo.broker_screen_insurance }}
							</view>
						</view>
					</view>
				</view>
				<view class="cart acea-row row-between-wrapper">
					<view class="title">租赁数量</view>
					<view> x 1</view>
				</view>
			</view>
		<!-- 	<view class="joinBnt bg-color"
				v-if="iSbnt && attr.productSelect.product_stock>0 &&attr.productSelect.quota>0" @click="goCat">我要参团
			</view> -->
<!-- 			<view class="joinBnt on"
				v-else-if="(iSbnt && attr.productSelect.quota<=0)||(iSbnt &&attr.productSelect.product_stock<=0)">已售罄
			</view> -->
			<view class="joinBnt bg-color" v-if="iScart && attr.productSelect.stock" @click="goCat">确定</view>
			<view class="joinBnt on" v-else-if="iScart && !attr.productSelect.stock">已售罄</view>
		</view>
		<view class="mask" @touchmove.prevent :hidden="attr.cartAttr === false" @click="closeAttr"></view>
		
		
		
	</view>
</template>

<script>
	import {
		getDayPrice
	} from '@/api/store.js';
	export default {

		props: {
			productValue: {
				type: Object
			},
			attr: {
				type: Object
			},
			storeInfo: {
				type: Object
			},
			limitNum: {
				type: Number,
				value: 0
			},
			isShow: {
				type: Number,
				value: 0
			},
			iSbnt: {
				type: Number,
				value: 0
			},
			iSplus: {
				type: Number,
				value: 0
			},
			iScart: {
				type: Number,
				value: 0
			},
			is_vip: {
				type: Number,
				value: 0
			},
			type: {
				type: String,
				default: ''
			},
			fangda: {
				type: Boolean,
				default: true
			},
			day_price: {
				type: String,
				default: ''
			},
			lease_mode: {
				type: String,
				default: ''
			},
			total_day: {
				type: String,
				default: ''
			},
			lease_mode_value: {
				type: String,
				default: ''
			},
			total_day_value: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				isFirst: true,
				attrTemp: {},
				day_price_temp: this.day_price,
				lease_mode_temp: this.lease_mode,
				total_day_temp: this.total_day,
				lease_mode_value_temp: this.lease_mode_value,
				total_day_value_temp: this.total_day_value,
				sku: ''
			};
		},
		watch: {
			attr: {
				handler(newVal, oldVal) {
					console.log('attrTempattrTempattrTempattrTemp')
					if (this.isFirst) {
						this.attrTemp = newVal
						this.isFirst = false
					} 
					let value = this
						.getCheckedValue()
						.join(",");
					this.sku = value
				}
			},
			// day_price: {
			// 	handler(newVal, oldVal) {
			// 		this.day_price_temp = newVal
			// 	}
			// },
			// lease_mode: {
			// 	handler(newVal, oldVal) {
			// 		console.log('99999999999999')
			// 		this.lease_mode_temp = newVal
			// 	}
			// },
			// total_day: {
			// 	handler(newVal, oldVal) {
			// 		this.total_day_temp = newVal
			// 	}
			// },
			// lease_mode_value: {
			// 	handler(newVal, oldVal) {
			// 		this.lease_mode_value_temp = newVal
			// 	}
			// },
			// total_day_value: {
			// 	handler(newVal, oldVal) {
			// 		this.total_day_value_temp = newVal
			// 	}
			// }
		},
		mounted() {
			
		},
		methods: {
			showLeased() {
				this.$emit('showLeased');
			},
			// getpreviewImage: function() {
			// 	uni.previewImage({
			// 		urls: this.attr.productSelect.image.split(','),
			// 		current: this.attr.productSelect.image
			// 	});
			// },

			goCat: function() {
				this.$emit('goCat');
			},
			/**
			 * 购物车手动输入数量
			 * 
			 */
			bindCode: function(e) {
				this.$emit('iptCartNum', this.attr.productSelect.cart_num);
			},
			closeAttr: function() {
				this.$emit('myevent');
			},
			CartNumDes: function() {
				this.$emit('ChangeCartNum', false);
			},
			CartNumAdd: function() {
				this.$emit('ChangeCartNum', true);
			},
			
			tapMode: function(indexn) {
				let that = this;
				this.$set(this, 'lease_mode_temp', this.storeInfo.lease_mode[indexn].id)
				this.$set(this, 'lease_mode_value_temp', this.storeInfo.lease_mode[indexn].name)
				that.$emit("attrVal", {
					lease_mode_value: this.lease_mode_value_temp,
					total_day_value: this.total_day_value_temp,
					total_day: this.total_day_temp,
					lease_mode: this.lease_mode_temp
				});
				
				let value = that
					.getCheckedValue()
					.join(",");
				
				this.sku = value
			
				that.$emit("ChangeAttr", value);

				let productSelect = this.productValue[value]
				
			
				if (productSelect) {
					const unique = productSelect.unique
					getDayPrice({
						product_id: this.storeInfo.id,
						product_attr_unique: unique,
						total_day: this.total_day_temp,
						lease_mode: this.lease_mode_temp,
						stage_day: this.storeInfo.stage_day
					}).then(res => {
						
						console.log('productSelectproductSelectproductSelectproductSelectproductSelectproductSelectproductSelect')
						
						this.$set(that, "day_price_temp", res.data.day_price)
					})
				}
				
				
				
			},
			tapDay: function(indexn) {
				let that = this;
				this.$set(this, 'total_day_temp', this.storeInfo.total_day[indexn].value)
				this.$set(this, 'total_day_value_temp', this.storeInfo.total_day[indexn].name)
				that.$emit("attrVal", {
					lease_mode_value: this.lease_mode_value_temp,
					total_day_value: this.total_day_value_temp,
					total_day: this.total_day_temp,
					lease_mode: this.lease_mode_temp,
				});
				
				let value = that
					.getCheckedValue()
					.join(",");
				
				this.sku = value
					
				that.$emit("ChangeAttr", value);
					
				let productSelect = this.productValue[value]
							
				if (productSelect) {
					const unique = productSelect.unique
					getDayPrice({
						product_id: this.storeInfo.id,
						product_attr_unique: unique,
						total_day: this.total_day_temp,
						lease_mode: this.lease_mode_temp,
						stage_day: this.storeInfo.stage_day
					}).then(res => {
						
							console.log('productSelectproductSelectproductSelectproductSelectproductSelectproductSelectproductSelect')
						
						this.$set(that, "day_price_temp", res.data.day_price)
					})
				}
				
			},
			
			tapAttr: function(indexw, indexn) {
				let that = this;
				that.$emit("attrVal", {
					indexw: indexw,
					indexn: indexn,
					lease_mode_value: this.lease_mode_value_temp,
					total_day_value: this.total_day_value_temp,
					total_day: this.total_day_temp,
					lease_mode: this.lease_mode_temp,
				    stage_day: this.storeInfo.stage_day
				});
				
				this.attrTemp.productAttr[indexw].index = this.attrTemp.productAttr[indexw].attr_values[indexn]
				// this.$set(this.attr.productAttr[indexw], 'index', this.attr.productAttr[indexw].attr_values[indexn])
				
				let value = that
					.getCheckedValue()
					.join(",");
					
				this.sku = value
				
				that.$emit("ChangeAttr", value);
				
				
				let productSelect = this.productValue[value]
			
				if (productSelect) {
					
					const unique = productSelect.unique
					
					getDayPrice({
						product_id: this.storeInfo.id,
						product_attr_unique: unique,
						total_day: this.total_day_temp,
						lease_mode: this.lease_mode_temp,
						stage_day: this.storeInfo.stage_day
					}).then(res => {
						this.$set(that, "day_price_temp", res.data.day_price)
					})
				}
				
			},
			//获取被选中属性；
			getCheckedValue: function() {
				let productAttr = this.attrTemp.productAttr;
				let value = [];
				for (let i = 0; i < productAttr.length; i++) {
					for (let j = 0; j < productAttr[i].attr_values.length; j++) {
						if (productAttr[i].index === productAttr[i].attr_values[j]) {
							value.push(productAttr[i].attr_values[j]);
						}
					}
				}
				return value;
			},
			showImg() {
				this.$emit('getImg');
			}
		}
	}
</script>

<style scoped lang="scss">
	.vip-money {
		color: #282828;
		font-size: 28rpx;
		font-weight: 700;
		margin-left: 6rpx;
	}

	.vipImg {
		width: 56rpx;
		height: 20rpx;
		margin-left: 6rpx;

		image {
			width: 100%;
			height: 100%;
			display: block;
		}
	}

	.product-window {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		background-color: #fff;
		z-index: 97;
		border-radius: 16rpx 16rpx 0 0;
		transform: translate3d(0, 100%, 0);
		transition: all .3s cubic-bezier(.25, .5, .5, .9);
		padding-bottom: 140rpx;
		padding-bottom: calc(140rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
	}

	.product-window.on {
		transform: translate3d(0, 0, 0);
	}

	.product-window.join {
		padding-bottom: 30rpx;
	}

	.product-window.joinCart {
		padding-bottom: 30rpx;
		z-index: 10000;
	}

	.product-window .textpic {
		padding: 0 30rpx 0 30rpx;
		margin-top: 29rpx;
		position: relative;
	}

	.product-window .textpic .pictrue {
		width: 176rpx;
		height: 176rpx;
		position: relative;
		.icon{
			width: 30rpx;
			height: 30rpx;
			background-color: rgba(0,0,0,0.4);
			border-radius: 4rpx;
			position: absolute;
			bottom: 8rpx;
			right: 8rpx;
			text-align: center;
			line-height: 23rpx;
			.iconfont{
				color: #fff;
				font-size: 20rpx;
			}
		}
	}

	.product-window .textpic .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.product-window .textpic .text {
		width: 490rpx;
		height: 176rpx;
		font-size: 32rpx;
		color: #202020;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		
	}

	.product-window .textpic .text .money {
		font-size: 24rpx;
		margin-top: 40rpx;
		color: #FF1F2E;
		.icon{
			display: inline-block;
			font-size: 16rpx;
			font-weight: normal;
			background: #FF9500;
			color: #fff;
			border-radius: 18rpx;
			padding: 2rpx 6rpx;
			margin-left: 10rpx;
			.iconfont{
				font-size: 16rpx;
				margin-right: 4rpx;
				color: #fff;
			}
		}
	}

	.sku {
		font-size: 24rpx;
		color: #666666;
		margin-top: 16rpx;
	}
	
	.leased {
		font-size: 24rpx;
		color: #666666;
		margin-top: 16rpx;
	}

	.product-window .textpic .text .money .num {
		font-size: 36rpx;
	}

	.product-window .textpic .text .money .stock {
		color: #999;
		margin-left: 6rpx;
	}

	.product-window .textpic .icon-guanbi {
		position: absolute;
		right: 30rpx;
		top: -5rpx;
		font-size: 35rpx;
		color: #8a8a8a;
	}

	.product-window .rollTop {
		max-height: 635rpx;
		overflow: auto;
		margin-top: 36rpx;
	}

	.product-window .productWinList .item~.item {
		margin-top: 36rpx;
	}

	.product-window .productWinList .item .title {
		font-size: 28rpx;
		color: #1F1F1F;
		padding: 0 30rpx;
	}

	.product-window .productWinList .item .listn {
		padding: 0 30rpx 0 16rpx;
	}

	.product-window .productWinList .item .listn .itemn {
		border: 1px solid #F2F2F2;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		font-size: 28rpx;
		color: #282828;
		padding: 0 30rpx;
		border-radius: 30rpx;
		margin: 20rpx 0 0 14rpx;
		background-color: #F2F2F2;
		word-break: break-all;
	}

	.product-window .productWinList .item .listn .itemn.on {
		color: #39CCC2;
		background: rgba(0,209,224,0.08);
		border: 1px solid #39CCC2;
	}

	.product-window .productWinList .item .listn .itemn.limit {
		color: #999;
		text-decoration: line-through;
	}

	.product-window .cart {
		margin-top: 36rpx;
		padding: 0 30rpx;
	}

	.product-window .cart .title {
		font-size: 28rpx;
		color: #1F1F1F;
	}

	.product-window .cart .carnum {
		height: 54rpx;
		margin-top: 24rpx;
	}

	.product-window .cart .carnum .iconfont {
		font-size: 25rpx;
	}

	.product-window .cart .carnum view {
		// border: 1px solid #a4a4a4;
		width: 84rpx;
		text-align: center;
		height: 100%;
		line-height: 54rpx;
		color: #282828;
		font-size: 45rpx;
	}

	.product-window .cart .carnum .reduce {
		border-right: 0;
		border-radius: 6rpx 0 0 6rpx;
		line-height: 48rpx;
		font-size: 60rpx;
	}

	.product-window .cart .carnum .reduce.on {
		// border-color: #e3e3e3;
		color: #DEDEDE;
	}

	.product-window .cart .carnum .plus {
		border-left: 0;
		border-radius: 0 6rpx 6rpx 0;
		line-height: 46rpx;
	}

	.product-window .cart .carnum .plus.on {
		// border-color: #e3e3e3;
		color: #dedede;
	}

	.product-window .cart .carnum .num {
		background: rgba(242, 242, 242, 1);
		color: #282828;
		font-size: 28rpx;
	}

	.product-window .joinBnt {
		font-size: 30rpx;
		width: 620rpx;
		height: 86rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 86rpx;
		color: #fff;
		margin: 21rpx auto 0 auto;
	}

	.product-window .joinBnt.on {
		background-color: #bbb;
		color: #fff;
	}
</style>